| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
echo 'CLEX configuration utility' |
| 4 |
echo '==========================' |
| 5 |
echo |
| 6 |
|
| 7 |
if [ x"`echo ~`" = x ] ; then |
| 8 |
echo 'Error: no home directory' |
| 9 |
exit 1 |
| 10 |
fi |
| 11 |
|
| 12 |
if [ x"$XDG_CONFIG_HOME" != x ] ; then |
| 13 |
clex_dir="$XDG_CONFIG_HOME/clex" |
| 14 |
else |
| 15 |
clex_dir=~/.config/clex |
| 16 |
fi |
| 17 |
clex_opt="$clex_dir/options" |
| 18 |
clex_cfg="$clex_dir/config" |
| 19 |
clex_bmk="$clex_dir/bookmarks" |
| 20 |
|
| 21 |
if [ -f "$clex_cfg" ] ; then |
| 22 |
echo "CLEX configuration file does exist. Please use the CLEX's" |
| 23 |
echo 'configuration panel to make changes.' |
| 24 |
echo |
| 25 |
echo 'If you want to create a new configuration from scratch, delete' |
| 26 |
echo "the file $clex_cfg and then run `basename $0` again." |
| 27 |
exit 1 |
| 28 |
fi |
| 29 |
|
| 30 |
if [ ! -d "$clex_dir" ] ; then |
| 31 |
if ! mkdir -p "$clex_dir" ; then |
| 32 |
echo "Error: could not create the $clex_dir directory" |
| 33 |
exit 1 |
| 34 |
fi |
| 35 |
fi |
| 36 |
|
| 37 |
restart_msg() { |
| 38 |
if [ x"$CLEX" != x ] ; then |
| 39 |
echo "Please restart CLEX" |
| 40 |
fi |
| 41 |
} |
| 42 |
|
| 43 |
# move files from 4.2, 4.3 or 4.4 ================== |
| 44 |
old_dir=~/.clex |
| 45 |
old_cfg="$old_dir/config" |
| 46 |
old_opt="$old_dir/options" |
| 47 |
old_bmk="$old_dir/bookmarks" |
| 48 |
|
| 49 |
if [ -f "$old_cfg" ] ; then |
| 50 |
echo 'moving the configuration file to new location' |
| 51 |
mv "$old_cfg" "$clex_cfg" |
| 52 |
fi |
| 53 |
if [ -f "$old_opt" ] ; then |
| 54 |
echo 'moving the options file to new location' |
| 55 |
mv "$old_opt" "$clex_opt" |
| 56 |
fi |
| 57 |
if [ -f "$old_bmk" ] ; then |
| 58 |
echo 'moving the bookmarks file to new location' |
| 59 |
mv "$old_bmk" "$clex_bmk" |
| 60 |
fi |
| 61 |
rmdir "$old_dir" 2>/dev/null |
| 62 |
if [ -f "$clex_cfg" ] ; then |
| 63 |
restart_msg |
| 64 |
exit 0 |
| 65 |
fi |
| 66 |
|
| 67 |
# move files from 4.0 or 4.1 ======================= |
| 68 |
old_cfg=~/.clexcfg |
| 69 |
old_opt=~/.clexopt |
| 70 |
old_bmk=~/.clexbm |
| 71 |
|
| 72 |
if [ -f "$old_cfg" ] ; then |
| 73 |
echo 'moving the configuration file to new location' |
| 74 |
mv "$old_cfg" "$clex_cfg" |
| 75 |
fi |
| 76 |
if [ -f "$old_opt" ] ; then |
| 77 |
echo 'moving the options file to new location' |
| 78 |
mv "$old_opt" "$clex_opt" |
| 79 |
fi |
| 80 |
if [ -f "$old_bmk" ] ; then |
| 81 |
echo 'moving the bookmarks file to new location' |
| 82 |
mv "$old_bmk" "$clex_bmk" |
| 83 |
fi |
| 84 |
if [ -f "$clex_cfg" ] ; then |
| 85 |
restart_msg |
| 86 |
exit 0 |
| 87 |
fi |
| 88 |
|
| 89 |
# convert files from 3.X if any ================== |
| 90 |
|
| 91 |
# list of CLEX4 variables to be converted from CLEX3 |
| 92 |
VARIABLE_LIST='C_PANEL_SIZE D_PANEL_SIZE H_PANEL_SIZE |
| 93 |
CMD_F3 CMD_F4 CMD_F5 CMD_F6 CMD_F7 CMD_F8 CMD_F9 CMD_F10 CMD_F11 CMD_F12 |
| 94 |
TIME_FMT DATE_FMT LAYOUT_ACTIVE LAYOUT1 LAYOUT2 LAYOUT3 |
| 95 |
CMD_LINES DIR2 FRAME KILOBYTE PROMPT QUOTE XTERM_TITLE' |
| 96 |
|
| 97 |
confirmation() |
| 98 |
{ |
| 99 |
echo -n "$1 (y/N) ? " |
| 100 |
read answer junk |
| 101 |
if [ x"$answer" != x'y' -a x"$answer" != x'Y' ] ; then |
| 102 |
echo "Exiting" |
| 103 |
exit 1 |
| 104 |
fi |
| 105 |
echo |
| 106 |
} |
| 107 |
|
| 108 |
reset_config() { |
| 109 |
local var |
| 110 |
|
| 111 |
for var in $VARIABLE_LIST ; do |
| 112 |
unset CLEX_$var |
| 113 |
done |
| 114 |
} |
| 115 |
|
| 116 |
read_config() { |
| 117 |
local line var val |
| 118 |
|
| 119 |
while read line ; do |
| 120 |
if ! echo $line | LC_ALL=C grep -q '^[A-Z][A-Z0-9_]*=.*$' ; then |
| 121 |
continue |
| 122 |
fi |
| 123 |
var=`echo $line | sed -e 's/=.*$//'` |
| 124 |
val=`echo $line | LC_ALL=C sed -e 's/^[A-Z0-9_]*=//'` |
| 125 |
eval CLEX_$var='"$val"' |
| 126 |
done < "$1" |
| 127 |
} |
| 128 |
|
| 129 |
convert_config() { |
| 130 |
echo 'Converting data from version 3 to version 4' |
| 131 |
if [ x"$CLEX_FMT_DATE" != x ] ; then |
| 132 |
CLEX_DATE_FMT=`echo $CLEX_FMT_DATE | sed -e 's/d/%d/;s/D/%e/;s/m/%m/;s/M/%b/;s/y/%y/;s/Y/%Y/'` |
| 133 |
fi |
| 134 |
if [ x"$CLEX_FMT_TIME" = x'1' ] ; then |
| 135 |
CLEX_TIME_FMT='%I:%M%p' |
| 136 |
elif [ x"$CLEX_FMT_TIME" = x'2' ] ; then |
| 137 |
CLEX_TIME_FMT='%H:%M' |
| 138 |
fi |
| 139 |
if [ x"$CLEX_ACTIVE_LAYOUT" != x ] ; then |
| 140 |
CLEX_LAYOUT_ACTIVE=`expr $CLEX_ACTIVE_LAYOUT + 1` |
| 141 |
fi |
| 142 |
if [ x"$CLEX_XTERM_TITLE" == x'2' ] ; then |
| 143 |
CLEX_XTERM_TITLE='1' |
| 144 |
fi |
| 145 |
if [ x"$CLEX_LAYOUT1" != x ] ; then |
| 146 |
CLEX_LAYOUT1=`echo $CLEX_LAYOUT1 | sed -e 's/| /|/'` |
| 147 |
fi |
| 148 |
if [ x"$CLEX_LAYOUT2" != x ] ; then |
| 149 |
CLEX_LAYOUT2=`echo $CLEX_LAYOUT2 | sed -e 's/| /|/'` |
| 150 |
fi |
| 151 |
if [ x"$CLEX_LAYOUT3" != x ] ; then |
| 152 |
CLEX_LAYOUT3=`echo $CLEX_LAYOUT3 | sed -e 's/| /|/'` |
| 153 |
fi |
| 154 |
} |
| 155 |
|
| 156 |
detect_programs() { |
| 157 |
local opt var val prog arg |
| 158 |
|
| 159 |
unset PROG3 PROG4 PROG5 PROG6 PROG7 PROG8 PROG9 |
| 160 |
ARG3='$f' |
| 161 |
ARG4='$f' |
| 162 |
if cp -- "$0" /dev/null >/dev/null 2>&1 ; then |
| 163 |
echo "End of options mark '--' is supported" |
| 164 |
opt=' --' |
| 165 |
PROG3='more' |
| 166 |
PROG4='vi' |
| 167 |
PROG5='cp -ir' ; ARG5='$f $2' |
| 168 |
PROG6='mv -i' ; ARG6='$f $2' |
| 169 |
PROG7='mkdir' ; ARG7='' |
| 170 |
PROG8='rm' ; ARG8='$f' |
| 171 |
PROG9='lpr' ; ARG9='$f' |
| 172 |
else |
| 173 |
echo "End of options mark '--' is NOT supported" |
| 174 |
opt='' |
| 175 |
fi |
| 176 |
|
| 177 |
if [ x"$CLEX_CMD_F3" = x ] ; then |
| 178 |
if [ x"$PAGER" != x ] ; then |
| 179 |
PROG3="$PAGER" |
| 180 |
elif less -V >/dev/null 2>&1; then |
| 181 |
PROG3='less' |
| 182 |
fi |
| 183 |
fi |
| 184 |
echo "Pager program: $PROG3" |
| 185 |
|
| 186 |
if [ x"$CLEX_CMD_F4" = x ] ; then |
| 187 |
if [ x"$EDITOR" != x ] ; then |
| 188 |
PROG4="$EDITOR" |
| 189 |
elif vim --version >/dev/null 2>&1; then |
| 190 |
PROG4='vim' |
| 191 |
fi |
| 192 |
fi |
| 193 |
echo "Text editor: $PROG4" |
| 194 |
|
| 195 |
for cmd in 3 4 5 6 7 8 9 ; do |
| 196 |
var=CLEX_CMD_F$cmd |
| 197 |
eval val="\$$var" |
| 198 |
if [ x"$val" != x ] ; then |
| 199 |
continue |
| 200 |
fi |
| 201 |
eval prog="\$PROG$cmd" |
| 202 |
if [ x"$prog" = x ] ; then |
| 203 |
continue |
| 204 |
fi |
| 205 |
eval arg="\$ARG$cmd" |
| 206 |
eval $var='"$prog$opt $arg"' |
| 207 |
done |
| 208 |
} |
| 209 |
|
| 210 |
print_config() { |
| 211 |
local var val |
| 212 |
|
| 213 |
for var in $VARIABLE_LIST ; do |
| 214 |
eval val="\$CLEX_$var" |
| 215 |
if [ x"$val" != x ] ; then |
| 216 |
echo "$var=$val" |
| 217 |
fi |
| 218 |
done |
| 219 |
} |
| 220 |
|
| 221 |
reset_config |
| 222 |
|
| 223 |
cfg3usr=~/.clexrc |
| 224 |
cfg3sys=/etc/clexrc |
| 225 |
cfg3alt=/usr/local/etc/clexrc |
| 226 |
clex3files='' |
| 227 |
for file in "$cfg3sys" "$cfg3alt" ; do |
| 228 |
if [ -f "$file" ] ; then |
| 229 |
echo 'System-wide configuration file from previous version 3 found' |
| 230 |
read_config "$file" |
| 231 |
clex3files="$clex3files $file" |
| 232 |
fi |
| 233 |
done |
| 234 |
if [ -f "$cfg3usr" ] ; then |
| 235 |
echo 'Personal configuration file from previous version 3 found' |
| 236 |
read_config "$cfg3usr" |
| 237 |
clex3files="$clex3files $cfg3usr" |
| 238 |
fi |
| 239 |
if [ x"$clex3files" != x ] ; then |
| 240 |
convert_config |
| 241 |
fi |
| 242 |
|
| 243 |
# build and save the configuration ============== |
| 244 |
|
| 245 |
detect_programs |
| 246 |
echo |
| 247 |
echo '=== BEGIN ==================' |
| 248 |
print_config |
| 249 |
echo '=== END ====================' |
| 250 |
echo |
| 251 |
|
| 252 |
confirmation 'Save this configuration' |
| 253 |
{ |
| 254 |
echo '#' |
| 255 |
echo '# CLEX configuration file' |
| 256 |
echo '#' |
| 257 |
print_config |
| 258 |
} > "$clex_cfg" |
| 259 |
if [ "$?" -ne 0 ] ; then |
| 260 |
echo 'Error saving configuration' |
| 261 |
exit 1 |
| 262 |
fi |
| 263 |
echo "Configuration saved" |
| 264 |
|
| 265 |
if [ x"$clex3files" != x ] ; then |
| 266 |
echo "You might now want to delete the old configuration file(s):$clex3files" |
| 267 |
fi |
| 268 |
restart_msg |
| 269 |
exit 0 |