initial checkin
[m6w6/repo-template] / init
1 #!/bin/bash
2 # - arguments will be exported to env
3 # - must called from within the repo root
4
5 export REPO=$(basename $(pwd) .git)
6
7 while test $# -gt 0
8 do
9 export "$1"
10 shift
11 done
12
13 for FILE in $(ls -a $(dirname -- $0)/presets)
14 do
15 SRCFILE=$(dirname -- $0)/presets/$FILE
16 case $FILE in
17 .|..)
18 continue
19 ;;
20 composer.json)
21 test "${COMPOSER+set}" = "" && continue
22 SRCFILE=$(mktemp -t $RANDOM)
23 php -d variables_order=E $(dirname -- $0)/presets/composer.json >$SRCFILE
24 ;;
25 README.md)
26 SRCFILE=$(mktemp -t $RANDOM)
27 php -d variables_order=E $(dirname -- $0)/presets/README.md >$SRCFILE
28 ;;
29 *)
30 ;;
31 esac
32
33 if test -e $FILE
34 then
35 DIFF="diff -udbBE -- $SRCFILE $FILE"
36 # ignore changes with only additions or removals
37 if ! $DIFF | awk '{if(NR<4){next}}/^-/{if(p){exit 1}m++}/^\+/{if(m){exit 1}p++}'
38 then
39 while read -n 1 -p "File $FILE exists and differs (q,s,r,d,v,?) " ACTION
40 do
41 echo
42 case $ACTION in
43 s)
44 break
45 ;;
46 r)
47 cp -fv -- $SRCFILE $FILE
48 break
49 ;;
50 d)
51 $DIFF
52 ;;
53 v)
54 vimdiff -- $SRCFILE $FILE
55 ;;
56 q)
57 exit
58 ;;
59 *)
60 echo "Select what to do now:"
61 echo " (q)uit and exit program now"
62 echo " (s)kip this file an keep the existing"
63 echo " (r)eplace the existing file with the preset"
64 echo " (d)iff the two files -udbBE"
65 echo " (v)imdiff the two files"
66 echo " (?) this help"
67 echo
68 ;;
69 esac
70 done
71 fi
72 else
73 cp -v $SRCFILE $FILE
74 fi
75 done