056f811be39881d6b888f6e6978d81b4d5af813b
[jigit.git] / jigit
1 #!/bin/bash
2 #
3 # jigit
4 #
5 # (c) 2004 Steve McIntyre
6 #
7 # Wrapper for jigdo to make updating CDs easier
8 #
9
10 # Some definitions
11 APT_CACHE=/var/cache/apt/archives
12 WGET_OPTS="-q --passive-ftp --no-directories --non-verbose"
13 HOST="http://people.debian.org/~93sam/"
14
15 if [ -e /etc/jigit.conf ] ; then
16     . /etc/jigit.conf
17 fi
18 if [ -e ~/.jigit.conf ] ; then
19     . ~/.jigit.conf
20 fi
21
22 URL="$HOST/jigit"
23
24 if [ "$TMPDIR"x = ""x ] ; then
25     TMPDIR=/tmp
26 fi
27
28 CDNAME=$1
29 case $CDNAME in
30     --help|-h)
31         echo "$0:"
32         echo "Automatic downloader for Jigdo images"
33         echo "Usage:"
34         echo "   jigit [distribution]"
35         exit 1
36     ;;
37 esac
38
39 for DIR in files jigdo
40 do
41     if [ ! -d $TMPDIR/jigit/$DIR ] ; then
42         mkdir -p -m 700 $TMPDIR/jigit/$DIR
43     fi
44 done
45
46 cd $TMPDIR/jigit/jigdo
47 # Grab the latest config file
48 echo "Downloading config:   $URL/$CDNAME.conf"
49 wget $WGET_OPTS --mirror $URL/$CDNAME.conf
50
51 . $CDNAME.conf
52 echo "Downloading jigdo:    $URL/$JIGDO"
53 wget $WGET_OPTS --mirror $URL/$JIGDO
54 echo "Downloading template: $URL/$TEMPLATE"
55 wget $WGET_OPTS --mirror $URL/$TEMPLATE
56
57 echo "If you have any previous CD or CD image(s) available, where are they mounted?"
58 echo "Say \"none\" if you have none; separate multiple entries with spaces"
59 read -p "> [none] " CD
60 if [ "$CD"x = ""x ] ; then
61     CD="none"
62 fi
63
64 cd $TMPDIR/jigit/files
65 # If we have a CD or iso, add it to the find list
66 if [ "$CD" != "none" ] ; then
67     FIND="$CD"
68 fi
69
70 # Now the local apt cache and our own temp directory
71 FIND="$FIND $APT_CACHE $TMPDIR/jigit/files"
72
73 > $TMPDIR/jigit/files/md5-list
74
75 for LOC in $FIND
76 do
77     echo
78     echo "Checking MD5 sums of files in $LOC:"
79     find $LOC -type f | \
80         xargs jigsum >> $TMPDIR/jigit/files/md5-list
81 done
82 echo
83 echo
84
85 rm -f $TMPDIR/jigit/jigdo/missing-list
86
87 # Check if we have all the pieces we need
88 mkimage -f $TMPDIR/jigit/files/md5-list \
89     -t $TMPDIR/jigit/jigdo/$TEMPLATE \
90     -j $TMPDIR/jigit/jigdo/$JIGDO \
91     -M $TMPDIR/jigit/jigdo/missing-list
92
93 # If we have a missing list, we're missing some files. Go and get them
94 if [ -e $TMPDIR/jigit/jigdo/missing-list ] ; then
95     NUM=`wc -l $TMPDIR/jigit/jigdo/missing-list | awk '{print $1}'`
96     echo "Need to download $NUM files to complete the image"
97     while [ $NUM -gt 0 ]
98     do
99         cd $TMPDIR/jigit/files
100         for file in `cat $TMPDIR/jigit/jigdo/missing-list`
101         do
102             printf "\r%5d files missing; retrieving %-45.45s" $NUM `basename $file`
103             mkdir -p `dirname $file`
104             wget $WGET_OPTS --mirror $URL/$SNAPSHOT/$file -O $file
105             SIZE=`stat -c %s $file`
106             if [ $SIZE -gt 0 ] ; then
107                 NUM=$(($NUM - 1))
108             else
109                 echo
110                 echo "Unable to download file $URL/$SNAPSHOT/$file to $file. Abort"
111                 exit 1
112             fi
113             jigsum $file >> $TMPDIR/jigit/files/md5-list 2>/dev/null
114         done
115     done
116
117     rm -f $TMPDIR/jigit/jigdo/missing-list
118         # Check (again) if we have all the pieces we need
119     mkimage -f $TMPDIR/jigit/files/md5-list \
120         -t $TMPDIR/jigit/jigdo/$TEMPLATE \
121         -j $TMPDIR/jigit/jigdo/$JIGDO \
122         -M $TMPDIR/jigit/jigdo/missing-list
123
124         # If we still have a missing list, something is wrong. Give up
125     if [ -e $TMPDIR/jigit/jigdo/missing-list ] ; then
126         echo "Failed to build image; could not find/download all the files. ABORT"
127         exit 1
128     fi
129 fi
130
131 printf "\r%5d files missing; all needed files available                       " 0
132 echo
133 echo
134 # We should have all the bits; build the image
135 mkimage -v -f $TMPDIR/jigit/files/md5-list \
136     -t $TMPDIR/jigit/jigdo/$TEMPLATE \
137     -j $TMPDIR/jigit/jigdo/$JIGDO \
138     -o $TMPDIR/jigit/jigdo/$CDNAME.iso \
139
140 if [ $? -eq 0 ] ; then
141     echo "Image created successfully in $TMPDIR/jigit/jigdo/$CDNAME.iso"
142 else
143     echo "Error creating image: $?"
144 fi