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