-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.sh
More file actions
163 lines (142 loc) · 7.37 KB
/
setup.sh
File metadata and controls
163 lines (142 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
echo
EXT_DIR="/home/xlshen/scratch"
BASH_DIR="$(realpath -L ${BASH_SOURCE[0]} | sed 's#/setup.sh##g')"
command echo -e "# +++++++++ loading \e[0;34m${BASH_DIR}/setup.sh:$LINENO \e[0;0m..."
echo -e "# EXT_DIR : \e[0;34m${EXT_DIR}\e[0;0m"
echo -e "# BASH_DIR : \e[0;34m${BASH_DIR}\e[0;0m"
echo
# create special folders
[[ ! -d ${EXT_DIR} ]] && {
command echo -e "\e[1;31mError : \e[1;34mSCRATCH_DIR does not exist: \e[0;33m${EXT_DIR}\e[0;0m"
return 1
}
function generate_path_config(){
local TEMP_DIR="${EXT_DIR}/tmp" # map to : "${EXT_DIR}/tmp/cache/docker_tmp/xxx"
local BACKUP_DIR="${EXT_DIR}/backup"
local CACHE_DIR="${EXT_DIR}/cache"
local TEST_DIR="${EXT_DIR}/test"
local DEPENDENCY_DIR="${EXT_DIR}/third_part_dependency"
[[ ! -d ${TEMP_DIR}/to_del ]] && mkdir -p ${TEMP_DIR}/to_del
[[ ! -d ${BACKUP_DIR} ]] && mkdir -p ${BACKUP_DIR}
[[ ! -d ${CACHE_DIR} ]] && mkdir -p ${CACHE_DIR}
[[ ! -d ${DEPENDENCY_DIR} ]] && mkdir -p ${DEPENDENCY_DIR}
[[ ! -d ${TEST_DIR} ]] && mkdir -p ${TEST_DIR} && cp ${BASH_DIR}/test/_test_bash.sh ${TEST_DIR}/
{
echo "#!/bin/bash"
echo "# auto-generated by $(realpath -L ${BASH_SOURCE[0]}):$LINENO"
echo "# init path : ${BASH_DIR}/init/script_path_config.sh"
echo "# It contains the path configuration for bash environment."
echo '[[ ! -v disable_bash_script_io ]] && echo -e "# +++++++++ loading ${BASH_SOURCE[0]}:${LINENO} \e[0;30m: ${BASH_SOURCE[1]##*/}:${BASH_LINENO[0]} \e[0;0m" '
echo
echo "export EXT_DIR=\"${EXT_DIR}\""
echo "export BASH_DIR=\"${BASH_DIR}\""
echo "export TEMP_DIR=\"${TEMP_DIR}\""
echo "export BACKUP_DIR=\"${BACKUP_DIR}\""
echo "export CACHE_DIR=\"${CACHE_DIR}\""
echo "export TEST_DIR=\"${TEST_DIR}\""
echo "export DEPENDENCY_DIR=\"${DEPENDENCY_DIR}\""
echo
echo '[[ ! -v disable_bash_script_io ]] && echo -e "# --------- leaving ${BASH_SOURCE[0]}:${LINENO} "'
} > ${BASH_DIR}/init/script_path_config.sh
command echo -e "# \e[0;34mPath config :\e[1;31m${BASH_DIR}/init/script_path_config.sh \e[0;0m"
cp ${BASH_DIR}/init/script_path_config.sh ${TEST_DIR}/
source ${BASH_DIR}/init/script_path_config.sh 1> /dev/null
# update python path variable file
cat ${BASH_DIR}/init/script_path_config.sh | grep -P "^ *#|^ *export |^ *$" | sed -E "s/^ *export +//" >| ${BASH_DIR}/app/python/utils/script_path_config.py
return 0
}
function reset_home_path(){
local target_path="$1"
# echo "reset_home_path : ${target_path}"
[[ -z $target_path || ! -e $target_path ]] && return 0
command echo -e "\e[0;31mRemove \e[0;35m ${target_path} \e[0;0m"
[[ -L ${target_path} ]] && rm -f ${target_path}
[[ -d ${target_path} ]] && rm -rf ${target_path}
[[ -f ${target_path} ]] && rm -rf ${target_path}
}
function generate_default_path_in_home() {
[[ $1 == 'reset' ]] && local reset_first=1 || local reset_first=0
echo -e "# HOME : \e[0;34m${HOME}\e[0;0m"
local home_cache="${HOME}/.cache"
[[ $reset_first -eq 1 ]] && reset_home_path "${home_cache}"
[[ ! -d ${home_cache} ]] && {
# ${HOME}/.cache , ${HOME}/.cache/docker_tmp
link_init_folder "${CACHE_DIR}/home_cache" ${home_cache} autogen
}
local home_vscode="${HOME}/.vscode-server"
[[ $reset_first -eq 1 ]] && reset_home_path "${home_vscode}"
[[ ! -d ${home_vscode} ]] && {
link_init_folder "${CACHE_DIR}/vscode-server" ${home_vscode} autogen
}
# ${HOME}/.local , ${HOME}/.local/lib
local home_local="${HOME}/.local"
# [[ $reset_first -eq 1 ]] && reset_home_path "${home_local}"
[[ ! -d ${home_local} ]] && {
link_init_folder "${CACHE_DIR}/home_local" ${home_local} autogen
}
local home_bash_history="${HOME}/.bash_history"
[[ $reset_first -eq 1 ]] && reset_home_path "${home_bash_history}"
[[ ! -f ${home_bash_history} ]] && {
link_init_file "${CACHE_DIR}/bash_history" ${home_bash_history} autogen
}
}
function link_init_file() {
[[ $3 == 'autogen' ]] && {
# if the file is not exist, we will create it automatically
# this is useful for auto-generated files, such as .bashrc, .profile, etc.
[[ ! -d $(dirname $1) ]] && { mkdir -p $(dirname $1) ; }
[[ ! -d $(dirname $2) ]] && { mkdir -p $(dirname $2) ; }
[[ ! -f $1 ]] && { echo "link_init_file : create $1" ; touch $1 ; }
}
# echo "\e[0;32mlink file : \e[0;35m$1 => \e[0;33m$2 \e[0;0m"
[[ -z $1 ]] && { echo " # link_init_file: first argument '$1' is empty!"; return 1; }
[[ -z $2 ]] && { echo " # link_init_file: second argument '$2' is empty!"; return 1; }
[[ ! -f $1 ]] && { echo " # link_init_file: first argument '$1' is not a file!"; return 1; }
[[ -d $2 ]] && { echo " # link_init_file: second argument '$2' is a directory, not a file!"; return 1; }
printf "\e[0;32m%-13s: \e[0;35m%-32s \e[0;0m<= \e[0;33m%s \e[0;0m\n" "link file" $2 $1
[[ -f $2 ]] && rm -f $2
[[ -L $2 ]] && rm -f $2
[[ -e $2 ]] && rm -rf $2
[[ -d $2 ]] && rm -rf $2
# echo "ln -s $1 $2"
# echo "ln -s $1 $2"
ln -s $1 $2
}
function link_init_folder() {
[[ $3 == 'autogen' ]] && {
# if the folder is not exist, we will create it automatically
# this is useful for auto-generated folders, such as .cache, .local, etc.
[[ ! -d $1 ]] && { echo -e "link_init_folder : Create $1" ; mkdir -p $1 ; }
}
# echo "\e[0;32mlink folder : \e[0;35m$1 => \e[0;33m$2 \e[0;0m"
[[ -z $1 ]] && { echo " # link_init_folder: first argument '$1' is empty!"; return 1; }
[[ -z $2 ]] && { echo " # link_init_folder: second argument '$2' is empty!"; return 1; }
[[ ! -d $1 ]] && { echo " # link_init_folder: first argument '$1' is not a directory!"; return 1; }
[[ -f $2 ]] && { echo " # link_init_folder: second argument '$2' is a file, not a directory!"; return 1; }
printf "\e[0;32m%-13s: \e[0;35m%-32s \e[0;0m<= \e[0;33m%s \e[0;0m\n" "link folder" $2 $1
[[ -d $2 ]] && rm -rf $2
ln -s $1 $2
}
generate_path_config || return 1
generate_default_path_in_home "reset" || return 2
link_init_file ${BASH_DIR}/init/bashrc ${HOME}/.bashrc
link_init_file ${BASH_DIR}/init/bashrc ${HOME}/.profile
link_init_file ${BASH_DIR}/app/gdb/gdb_init.gdb ${HOME}/.gdbinit
link_init_file ${BASH_DIR}/app/gdb/cuda_gdb/cuda-gdbinit.gdb ${HOME}/.cuda-gdbinit
link_init_file ${BASH_DIR}/app/python/pdb/python_pdb_init.py ${HOME}/.pdbrc
link_init_file ${BASH_DIR}/app/git/gitconfig ${HOME}/.gitconfig
link_init_file ${BASH_DIR}/app/gdb/test/ddd_init.gdb ${HOME}/.dddinit
link_init_folder ${CACHE_DIR} ${HOME}/.cache
link_init_folder ${TEMP_DIR} ${HOME}/temp
link_init_folder ${DEPENDENCY_DIR} ${HOME}/.local
#
# find ${HOME} -maxdepth 1 -type f -iname "*history" -print
# ${HOME}/.history
# ${HOME}/.bash_history
# ${HOME}/.gdb_history
# ${HOME}/.pdbhistory
# ${HOME}/.python_history
# ${HOME}/.zshrc
# ${HOME}/.gitconfig
command echo -e "# --------- leaving \e[0;34m${BASH_DIR}/setup.sh:$LINENO \e[0;0m"
echo