install.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/system/bin/sh
  2. # Config and runtime directory (if changed, also change in cjdaemon and 99cjdroute)
  3. CJDPATH="/sdcard/cjdns"
  4. # Partition/disk the $BINDIR and $INITDIR are on
  5. SYSDISK="/system"
  6. # The folder to place executables (should be in $PATH)
  7. BINDIR="$SYSDISK"/bin
  8. # The init.d folder the version of Android on your device expcets to see init.d scripts
  9. INITDIR="$SYSDISK"/etc/init.d
  10. # Change to the script directory
  11. cd `dirname "$0"`
  12. # Set the name of this script
  13. APPNAME="${0##*/}"
  14. function _sysrw() {
  15. # Remount $SYSDISK read/write if necessary
  16. mount | grep " $SYSDISK " | grep ro >/dev/null \
  17. && REMOUNT=1 \
  18. && mount -o rw,remount "$SYSBLOCK" "$SYSDISK"
  19. if [ "$REMOUNT" = 1 ]; then
  20. mount | grep " $SYSDISK " | grep ro >/dev/null \
  21. && echo -e "Error: couldn't remount ${SYSDISK} read/write\n" \
  22. && exit 1
  23. fi
  24. }
  25. function _sysro() {
  26. # Remount $SYSDISK read-only if it was previously remounted
  27. if [ "$REMOUNT" = 1 ]; then
  28. mount | grep " $SYSDISK " | grep rw >/dev/null \
  29. && mount -o ro,remount "$SYSBLOCK" "$SYSDISK"
  30. mount | grep " $SYSDISK " | grep rw >/dev/null \
  31. && echo "Error: couldn't remount ${SYSDISK} read-only"
  32. fi
  33. }
  34. # The help output functionality
  35. if [ -n "$1" ]; then
  36. if [ "$1" = "-h" -o "$1" = "--help" ]; then
  37. echo -e "\nUsage:\n\t${APPNAME} [option]\n"
  38. echo -e "\t* This script requires root permissions to run"
  39. echo -e "\t* Run this script with no arguments to install cjdns"
  40. echo -e "\t* Install cjdns again after flashing new/upated ROMs"
  41. echo -e "\nOptions:"
  42. echo -e "\t-u|--uninstall: uninstall from ${SYSDISK}"
  43. echo -e "\t-h|--help: display this help output\n"
  44. exit 0
  45. fi
  46. fi
  47. # Fail if user isn't root (it's needed from here on out)
  48. if [ ! `whoami` = "root" ]; then
  49. echo
  50. echo "Error: this script must be run as root"
  51. echo
  52. exit 1
  53. fi
  54. # Check that /dev/net/tun exists and create it if it doesn't
  55. if [ ! -c /dev/tun ]; then
  56. mknod /dev/tun c 10 200
  57. fi
  58. # Set the device block for the system partition
  59. SYSBLOCK=`mount | grep " $SYSDISK " | grep -oe "^[^\ ]*" | head -n 1`
  60. if [ ! -e "$SYSBLOCK" ]; then
  61. echo
  62. echo "Error: Couldn't detect the device ${SYSDISK} is mounted on"
  63. echo
  64. exit 1
  65. fi
  66. # Stop cjdroute if it's running
  67. if [ `pgrep cjdroute | wc -l` -gt 0 ]; then
  68. echo
  69. echo "Killing cjdroute..."
  70. echo
  71. killall cjdroute
  72. fi
  73. # The uninstall functionality and a catch for invalid arguments
  74. if [ -n "$1" ]; then
  75. if [ "$1" = "-u" -o "$1" = "--uninstall" ]; then
  76. # Remount the system partition read/write
  77. _sysrw
  78. echo "Removing cjdroid files from the ${SYSDISK} partition..."
  79. # Remove cjdaemon
  80. if [ -f "$BINDIR"/cjdaemon ]; then
  81. rm "$BINDIR"/cjdaemon
  82. else
  83. echo " Warning: ${BINDIR}/cjdaemon is not present to be removed"
  84. fi
  85. # Remove cjdctl
  86. if [ -f "$BINDIR"/cjdctl ]; then
  87. rm "$BINDIR"/cjdctl
  88. else
  89. echo " Warning: ${BINDIR}/cjdctl is not present to be removed"
  90. fi
  91. # Remove cjdroute
  92. if [ -f "$BINDIR"/cjdroute ]; then
  93. rm "$BINDIR"/cjdroute
  94. else
  95. echo " Warning: ${BINDIR}/cjdroute is not present to be removed"
  96. fi
  97. # Remove 99cjdroute
  98. if [ -f "$INITDIR"/99cjdroute ]; then
  99. rm "$INITDIR"/99cjdroute
  100. else
  101. echo " Warning: ${INITDIR}/99cjdroute is not present to be removed"
  102. fi
  103. # Remount the system partition read-only
  104. _sysro
  105. # Exit successfully if all the cjdroid files are gone, otherwise complain
  106. if [ ! -f "$BINDIR"/cjdaemon -a ! -f "$BINDIR"/cjdctl -a ! -f "$BINDIR"/cjdroute -a ! -f "$INITDIR"/99cjdroute ]; then
  107. echo
  108. echo "Uninstallation successfully completed!"
  109. echo
  110. exit 0
  111. else
  112. echo "Error: not all files were removed"
  113. echo
  114. exit 1
  115. fi
  116. else
  117. echo
  118. echo "Error: ${1} is not a valid argument"
  119. echo
  120. exit 1
  121. fi
  122. fi
  123. # Remount the system partition read/write
  124. _sysrw
  125. # Copy cjdns-related-files to $SYSDISK
  126. echo
  127. echo "Copying files to the ${SYSDISK} partition..."
  128. if [ ! -f "files/cjdaemon" -o ! -f "files/cjdctl" -o ! -f "files/cjdroute" -o ! -f "files/99cjdroute" ]; then
  129. echo "Error: one or more of the required files in 'files/' are missing"
  130. echo
  131. exit 1
  132. fi
  133. install -D -m755 files/cjdaemon "$BINDIR"/cjdaemon
  134. install -D -m755 files/cjdctl "$BINDIR"/cjdctl
  135. install -D -m755 files/cjdroute "$BINDIR"/cjdroute
  136. install -D -m755 files/99cjdroute "$INITDIR"/99cjdroute
  137. # Remount the system partition read-only
  138. _sysro
  139. # Exit successfully if all the files are where they should be, otherwise complain
  140. if [ -f "$BINDIR"/cjdaemon -a -f "$BINDIR"/cjdctl -a -f "$BINDIR"/cjdroute -a -f "$INITDIR"/99cjdroute ]; then
  141. # Create the config directory if it doesn't already exist
  142. if [ ! -d "$CJDPATH" ]; then
  143. echo
  144. echo "Creating cjdns config folder @ ${CJDPATH}..."
  145. echo
  146. install -d "$CJDPATH"
  147. fi
  148. # Generate a config file if the user doesn't already have one
  149. if [ ! -f "$CJDPATH"/cjdroute.conf ]; then
  150. echo
  151. echo "Creating cjdns config file @ ${CJDPATH}/cjdroute.conf..."
  152. echo
  153. /system/bin/cjdroute --genconf > "$CJDPATH"/cjdroute.conf
  154. fi
  155. echo
  156. echo "Installation successfully completed!"
  157. echo " Ensure ${CJDPATH}/cjdroute.conf is configured then"
  158. echo " run 'cjdctl enable' to start the cjdroute service"
  159. echo
  160. exit 0
  161. else
  162. echo "Error: not all files were successfully copied"
  163. echo
  164. echo " Hint: Try rebooting then re-running this script"
  165. echo
  166. exit 1
  167. fi