thm - scripts - various script and utils
 (HTM) git clone git://z3bra.org/scripts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       thm (3362B)
       ---
            1 #!/bin/sh
            2 #
            3 # z3bra - (c) wtfpl 2014
            4 # Manage your config.h files on a per program basis. Store defaults and user
            5 # configs, and restore them easily.
            6 
            7 # Directory where configs are saved
            8 basedir=~/.hm.d
            9 
           10 # Default names for configs
           11 default=config.def.h
           12 
           13 # How to list files managed by hmgr
           14 #listcmd='ls -1 --color=auto'
           15 listcmd='tree -L 2 --noreport'
           16 
           17 # Change output colors ?
           18 color_conf='1;37' # colors for config files
           19 color_dirs='0;33' # colors for directory names
           20 
           21 
           22 usage() {
           23 echo "usage: $(basename $0) [-hu] [-l [dir]] [-di <file>] [-csr <dir>/<name>]"
           24 
           25 test -z "$1" && return
           26 
           27 cat <<EOF
           28     -h              : help
           29     -u              : shortcut for -n ${USER}.h
           30 
           31     -l [dir]        : list currently managed applications/files
           32 
           33     -d <dir>/<name> : remove <name> from managed configs
           34     -i <file>       : input file to use (defaults to ./config.h)
           35 
           36     -c <dir>        : check which config is in use
           37     -s <dir>/<name> : store \`config.h\` to <dir>/<name>
           38     -r <dir>/<name> : restore \`config.h\` from <dir>/<name>
           39 EOF
           40 }
           41 
           42 store() {
           43     test -z "$1" && return 1
           44 
           45     # check if the user gave a filename or not
           46     # and deduct filepath from that
           47     if test `dirname $1` = '.'; then
           48         dir=$1
           49         filepath=${basedir}/${dir}/${default}
           50     else
           51         dir=`dirname $1`
           52         filepath=${basedir}/$1
           53     fi
           54 
           55     # create directory if it does not exist
           56     test ! -d ${basedir}/${dir} && mkdir -p ${basedir}/${dir}
           57 
           58     # Copy from current dir to base directory
           59     cp -i ${cin} ${filepath}
           60 }
           61 
           62 restore() {
           63     test -z "$1" && return 1
           64 
           65     if test -f ${basedir}/$1; then
           66         filepath=${basedir}/$1
           67     else
           68         filepath=${basedir}/$1/${default}
           69     fi
           70 
           71     # Copy from base dir to current directory
           72     cp ${filepath} ${cin}
           73 }
           74 
           75 list() {
           76 
           77     # Go to the base directory
           78     cd ${basedir}
           79 
           80     ${listcmd} $1
           81 }
           82 
           83 check() {
           84     found=0
           85 
           86     test ! -f ${cin} && echo "cannot find file ${cin}" && exit 1
           87 
           88     for dir in ${basedir}/* ; do
           89         for file in ${dir}/*; do
           90             if diff $file ${cin} >/dev/null 2>&1; then
           91                 printf "${fgd}`basename ${dir}`${nofg}/"
           92                 printf  "${fgh}`basename ${file}`${nofg}\n"
           93                 found=1
           94             fi
           95         done
           96     done
           97     test ${found} -eq 0 && echo ${cin} is different from stored configs
           98 }
           99 
          100 # No arguments? give usage
          101 test $# -eq 0 && list && exit 0
          102 
          103 # Create $basedir if it does not exists
          104 test ! -d ${basedir} && mkdir -p ${basedir}
          105 
          106 # Set the default file names
          107 cin=config.h
          108 list=0
          109 
          110 # standardize colors for shell output
          111 fgd="\e[${color_dirs}m"
          112 fgh="\e[${color_conf}m"
          113 nofg="\e[0m"
          114 
          115 # change colors, for fun!
          116 LS_COLORS="di=${color_dirs}:*.h=${color_conf}"
          117 export LS_COLORS
          118 
          119 # Parse options
          120 while getopts "chi:ld:s:r:u" opt; do
          121     case $opt in
          122         # Check which config is in use
          123         c) check;;
          124 
          125         # Wipe the config given as argument
          126         d) rm ${basedir}/$OPTARG;;
          127 
          128         # Change the input file
          129         i) cin=$OPTARG;;
          130 
          131         # List currently managed config.h
          132         l) list=1; break;;
          133 
          134         # Whether to store or restore a config.h
          135         s) store $OPTARG;;
          136         r) restore $OPTARG;;
          137 
          138         # WHAAT?!
          139         h) usage full; exit 0;;
          140         *) usage; exit 1;;
          141     esac
          142 done
          143 
          144 # In case we want to list files managed...
          145 shift $(( OPTIND - 1 ))
          146 
          147 # List either the whole dir or a specific one
          148 test $list -eq 1 && list $1